package com.yulebaby.callsenter.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.orhanobut.logger.Logger;
import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.ActualTranProgressBean;
import com.yulebaby.callsenter.bean.TranProgressTargetBean;

import java.util.List;

/**
 * Author: Zhangbo
 * Time: 2017/1/22
 */
public class AddTargetAdapter extends BaseQuickAdapter<TranProgressTargetBean, BaseViewHolder> {
    public AddTargetAdapter(List<TranProgressTargetBean> data) {
        super(R.layout.item_add_target, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, TranProgressTargetBean bean) {
        TextView tvTop = baseViewHolder.getView(R.id.tv_steptop);
        TextView tvBottom = baseViewHolder.getView(R.id.tv_stepbottom);
        EditText target = baseViewHolder.getView(R.id.et_target);

        //1、为了避免TextWatcher在第2步被调用，提前将他移除。
        if (target.getTag() instanceof TextWatcher) {
            target.removeTextChangedListener((TextWatcher) (target.getTag()));
        }

        switch (bean.getTrack_pro()) {
            case 2:
                tvTop.setText("步骤1: 确认客户意向");
                tvBottom.setText("步骤2: 客户明确表述需求");
                break;
            case 3:
                tvTop.setText("步骤2: 客户明确表述需求");
                tvBottom.setText("步骤3: 已阅览资料或参观店面");
                break;
            case 4:
                tvTop.setText("步骤3: 已阅览资料或参观店面");
                tvBottom.setText("步骤4: 明确知晓项目优势");
                break;
            case 5:
                tvTop.setText("步骤4: 明确知晓项目优势");
                tvBottom.setText("步骤5: 客户到访");
                break;
            case 6:
                tvTop.setText("步骤5: 客户到访");
                tvBottom.setText("步骤6: 已签约商铺");
                break;
            case 7:
                tvTop.setText("步骤6: 已签约商铺");
                tvBottom.setText("步骤7: 签合同并打款到账");
                break;
        }

        switch (bean.getTran_targetnum()) {
            case -2:
                target.setText("");
                target.setBackgroundResource(R.drawable.edit_backgroud_error);
                break;
            default:
                target.setText(bean.getTran_targetnum() == -1 ? "" : String.valueOf(bean.getTran_targetnum()));
                target.setBackgroundResource(R.drawable.edit_backgroud_normal);
                break;
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    bean.setTran_targetnum(Integer.valueOf(s.toString().trim()));
                } else if (bean.getTran_targetnum() == -2) {

                } else {
                    bean.setTran_targetnum(-1);
                }
            }
        };
        target.addTextChangedListener(watcher);
        target.setTag(watcher);

    }
}
